package cn.zebra.dev.admin.module.product.controller;

import cn.zebra.dev.admin.module.login.SecurityAdminService;
import cn.zebra.dev.admin.module.product.dto.ProductGroupDTO;
import cn.zebra.dev.admin.module.product.vo.ProductGroupQueryVO;
import cn.zebra.dev.admin.module.product.vo.ProductGroupVO;
import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.common.utils.ObjectUtils;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.product.module.group.entity.GroupEntity;
import cn.zebra.dev.product.module.group.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RequestMapping("/product/group")
@RestController
@Api(tags = "商品分组管理")
public class ProductGroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private SecurityAdminService adminService;

    @GetMapping("/get/{id}")
    @ApiOperation("获取商品分组信息")
    public Result<ProductGroupDTO> get(@PathVariable Long id) {
        GroupEntity entity = groupService.findById(id);
        if (entity == null){
            return Result.error("不存在该商品分组");
        }
        ProductGroupDTO dto = new ProductGroupDTO();
        BeanUtils.copyProperties(entity,dto);
        return Result.success(dto);
    }

    @GetMapping("/list")
    @ApiOperation("商品分组列表")
    public Result<PageUtils<ProductGroupDTO>> list(
            ProductGroupQueryVO vo,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> map = ObjectUtils.objectConvertMap(vo);
        map.put(ParamsConstant.LIMIT, limit);
        map.put(ParamsConstant.PAGE, page);
        PageUtils<GroupEntity> pageUtils = groupService.findPage(map);
        PageUtils<ProductGroupDTO> data = new PageUtils<>();
        List<GroupEntity> list = pageUtils.getList();
        if (list.size()> 0){
            List<ProductGroupDTO> dtoList = new LinkedList<>();
            list.forEach(entity -> {
                ProductGroupDTO dto = new ProductGroupDTO();
                BeanUtils.copyProperties(entity,dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

    @PostMapping("/save")
    @ApiOperation("保存商品分组")
    public Result save(@Valid @RequestBody ProductGroupVO vo) {
        GroupEntity entity = new GroupEntity();
        BeanUtils.copyProperties(vo, entity);
        if (entity.getParentId() == null) {
            entity.setParentId(0L);
        }
        if (groupService.saveProductGroup(entity, adminService.getId())) {
            return Result.success("添加成功!");
        }
        return Result.error("保存失败!");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改商品分组")
    public Result save(@PathVariable Long id, @Valid @RequestBody ProductGroupVO vo) {
        GroupEntity entity = new GroupEntity();
        BeanUtils.copyProperties(vo, entity);
        if (entity.getParentId() == null) {
            entity.setParentId(0L);
        }
        if (groupService.updateProductGroup(id, entity, adminService.getId())) {
            return Result.success("修改成功!");
        }
        return Result.success("修改失败!");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除商品分组")
    public Result delete(@PathVariable Long id) {
        GroupEntity entity = new GroupEntity();
        entity.setDeleteFlag(true);
        if (groupService.update(entity, "id = {0}", id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败!");
    }
}
